var _positionedJP = {};
// Mitu laeva paigutatud (JP)
var _positionedJPNum =0;
var _illegal = {};
var _illegalByID = {};
var _beenTargeted = {};
var deployed = 0;

function getShipLocation()
{
	return _positionedJP;
}
function getPositionedJP ()
{
	temp = {};
	$.each(_positionedJP,function(key,val)
			{
				temp[key]=val.length;
			});
	return temp;
}
function getCells ()
{
	cells = {};
	$.each(_positionedJP,function(key,val)
			{
				for(i=0;i<val.length;i++)
					{
					cells[String(val[i])]=key;
					}
			});
	
	return cells;
}
function isPositioned(id)
{

	if (_positionedJP[id]!=null)
	{
		return true;
	}
	else
	{
		return false;
	}	
}
// eemaldab laeva positsiooni ja positioneeritud laevade arvu
function removePosition(id)
{
		_positionedJPNum-=1;
		_positionedJP[id]=null;	
}
//Annab tagasi, kas koik laevad on paigutad, argement maarab poole(0 jp,1 us)
function isDeployed()
{
	return _positionedJPNum;
}
function shotValid(id)
{
	if (_beenTargeted[id]==null)
	{
		_beenTargeted[id]=1;
		return true;
	}
	else
	{
		return false;
	}
}
function shipBorders(loc,id)
//Finds and returns ship border areas
{
	if (typeof loc == "string")
	{
		var str = loc.slice(2,-2);
		var arrayStr = str.split("],[");
		var tempArray = [];
		for (i=0;i<arrayStr.length;i++)
			{
			tempArray[tempArray.length]=arrayStr[i].split(",").map(function( num ){ return parseInt( num, 10 );});
			}
		var loc=tempArray;
	}
	var illegal = [];
	for(j=0;j<loc.length;j++)
		{
		illegalG(loc[j]);
		}
	if (loc[0][1]==loc[loc.length-1][1])
	{
		for ((i=(loc[0][0])-1);i<=(loc[loc.length-1][0]+1);i++)
		{
			if (((loc[0][1]-1)>0) &&  (i<11) && (i>0))
			{
				var temp = [i,loc[0][1]-1];
				illegal[illegal.length]=temp;
				illegalG(temp);
				
			}
			if (((loc[0][1]+1)<11) &&  (i<11) && (i>0))
			{
				var temp = [i,loc[0][1]+1];
				illegal[illegal.length]= temp;
				illegalG(temp);
			}
		
		}
		if (loc[0][0]-1>0)
		{
			illegal[illegal.length]=[loc[0][0]-1,loc[0][1]];
			illegalG([loc[0][0]-1,loc[0][1]]);
		}
		if (loc[loc.length-1][0]+1<11)
		{
			illegal[illegal.length]=[loc[loc.length-1][0]+1,loc[0][1]];
			illegalG([loc[loc.length-1][0]+1,loc[0][1]]);
		}
	}
	else
	{
		for ((i=(loc[0][1])-1);i<=(loc[loc.length-1][1]+1);i++)
		{
			if (((loc[0][0]-1)>0) &&  (i<11) && (i>0))
			{
				illegal[illegal.length]=[loc[0][0]-1,i];
				illegalG([loc[0][0]-1,i]);
			}
			if (((loc[0][0]+1)<11) &&  (i<11) && (i>0))
			{
				illegal[illegal.length]=[loc[0][0]+1,i];
				illegalG([loc[0][0]+1,i]);
			}
		
		}
		if (loc[0][1]-1>0)
		{
			illegal[illegal.length]=[loc[0][0],loc[0][1]-1];
			illegalG([loc[0][0],loc[0][1]-1]);
		}
		if (loc[loc.length-1][1]+1<11)
		{
			illegal[illegal.length]=[loc[0][0],loc[loc.length-1][1]+1];
			illegalG([loc[0][0],loc[loc.length-1][1]+1]);
		}
	}
	var illegalO = illegal.slice(0);
	illegalO = illegalO.concat(loc);
	if (id!=null)
	{
		_illegalByID[id]=illegalO;
	}
	
	return illegal;
}
// kontrollib, kas laeva alla jaavad ruudud on vabad
function isValid (arr)
{
	for (i=0;i<arr.length;i++)
	{
		var elem = InttoS(arr[i]);
		if ((_illegal[elem]==null) || (_illegal[elem]==0))
		{
		}
		else
		{
			return false;
		}
	}
	return true;
}
// Lisab mittesobivad valjad jarjendisse
function illegalG(arr)
{
	if ((_illegal[InttoS(arr)]==null) || (_illegal[InttoS(arr)]==0))
		{
		_illegal[InttoS(arr)]=1;
		}
	else
		{
		
		_illegal[InttoS(arr)]=_illegal[InttoS(arr)]+1;
		}

}
// Annab tagasi koik kinnised valjad
function allIllegal()
{
	var fields= [];
	var ships= [];
	$.each(_illegal,function(key,val)
		{
			if (val != null && val > 0)
			{
				fields[fields.length]=key;
			}
		});
	// leiame need, mis on laevade all ja lahutame
	$.each(_positionedJP,function(key,val)
		{
			ships = ships.concat(val);
		});
	ships = ArrayIntoS(ships);
	Array.prototype.diff = function(a) {
	    return this.filter(function(i) {return a.indexOf(i) <0;});
	};

	var fieldsWithoutShip = fields.diff(ships);
	for (i=0;i<fieldsWithoutShip.length;i++)
		{
		fieldsWithoutShip[i]=StoInt(fieldsWithoutShip[i]);
		}

	
	return fieldsWithoutShip;
}
// Attab laeva pikkuse(kasutatakse tema alla jaavate ruutude leidmiseks
function lenInS (obj)
{
	id1="#"+obj.id;
	JQ = $(id1);
	if (JQ.width() > JQ.height())
	{
		len = JQ.width() /  $(".square").width();
		len  = Math.round(len);
	}
	else
	{
		len = JQ.height() /  $(".square").height();
		len  = Math.round(len);
	}
	if (len>4)
	{
		len=4;
	}
	return len;
}
function StoInt(SId)
{
	var temp = SId.split(",");
	return [parseInt(temp[0].slice(1)), parseInt(temp[1])];
}
function InttoS(sArr)
{
	return "L"+String(sArr[0])+","+String(sArr[1]);
}
function ArrayIntoS(sArr)
{
	for (i=0;i<sArr.length;i++)
		{
			sArr[i]=InttoS(sArr[i]);
		}
	return sArr;
}
function legalize(id) 
{
	toBeCleaned=[];
	arr = _illegalByID[id];
	if (arr==null)
	{
		return [false];
	}
	for (i=0;i<arr.length;i++)
	{
		_illegal[InttoS(arr[i])]=_illegal[InttoS(arr[i])]-1;
		if (_illegal[InttoS(arr[i])]==0)
		{
			toBeCleaned[toBeCleaned.length]=arr[i];
		}	
	}
	_illegalByID[id]=null;
	_positionedJPNum -=1;
	
	return [true,toBeCleaned];
}
function validator (SqPos,obj,rot)
{
	var valid = true;
	len = lenInS(obj);
	if (typeof SqPos=="string")
	{
	var SqPos = StoInt(SqPos);
	}
	var loc=[];
	var isRot=1;
	if (rot!=null)
	{
		isRot=0;
	}
	for (i=SqPos[isRot];i<=(SqPos[isRot]+len-1);i++)
	{
		var scndCor = SqPos[(isRot+1) % 2];
		if (isRot==1){
			loc[loc.length]=[scndCor,i];
		}
		else
		{
			loc[loc.length]=[i,scndCor];
		}
	}
	// kui indeks on liiga suur(manguvaljalt valjas), tagasta false
	valid = isValid(loc);
	if (((loc[(loc.length)-1][1])>10 || (loc[(loc.length)-1][0])>10) || valid==false)
	{
		return [false];
	}
	else
	{
		_positionedJPNum+=1;
		_positionedJP[obj.id]=loc;
		return [true,shipBorders(loc,obj.id)];
	}
}
function validateFor90DegreeTurn(element,turned)
{
	var id = element.id;
	var cellsPositionedIn = _positionedJP[id];
	if (cellsPositionedIn==null)
	{
		return [true,null];
	}
	else
	{
		
		if (turned==null || turned == false)
		{
			turnedAlt=1;
		}
		else
		{
			turnedAlt=null;
		}
		var legalized = legalize(id);
		var validatorResult = validator(cellsPositionedIn[0],element,turnedAlt);
		validatorResult[2]=legalized[1];
		if (validatorResult[0]==false)
		{
			validatorResult[1]=cellsPositionedIn[0];
		}
		return validatorResult;
	
		
	}
}